<?php

/**
 * This is the model class for table "tbl_profesores".
 *
 * The followings are the available columns in table 'tbl_profesores':
 * @property integer $id_asesorinterno
 * @property string $activo
 *
 * The followings are the available model relations:
 * @property TblResidencias[] $tblResidenciases
 */
class Profesores extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public $nombres;
	public $id_usuario;
	public $apellidop;
	public $apellidom;
	public function tableName()
	{
		return 'tbl_profesores';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('activo', 'length', 'max'=>1),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_asesorinterno, activo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbl_Residencias' => array(self::HAS_MANY, 'TblResidencias', 'id_asesorinterno'),
			//'Usuarios.Usuarios'=> array(self::HAS_ONE,'Usuarios.usuarios','id_usuario' ),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_asesorinterno' => 'Id Asesorinterno',
			'activo' => 'Activo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_asesorinterno',$this->id_asesorinterno);
		$criteria->compare('activo',$this->activo,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Profesores the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function getAsesoresInternos()
		{
			
			/*$sql = "SELECT usuarios.usuarios.id_usuario,usuarios.usuarios.nombres,
					usuarios.usuarios.apellidop, usuarios.usuarios.apellidom,dbresidencia.tbl_profesores.activo
					FROM usuarios.usuarios, dbresidencia.tbl_profesores
					WHERE dbresidencia.tbl_profesores.id_asesorinterno=usuarios.usuarios.id_usuario";*/
		 //sql serv

			/*$sql = "SELECT usuariosxii.usuarios.id_usuario,usuariosxii.usuarios.nombres,
					usuariosxii.usuarios.apellidop, usuariosxii.usuarios.apellidom,dbresidencias.tbl_profesores.activo
					FROM usuariosxii.usuarios, dbresidencias.tbl_profesores
					WHERE dbresidencias.tbl_profesores.id_asesorinterno=usuariosxii.usuarios.id_usuario";	*/	
			
								
			$data_profesores = $this->findAll();

			$llaves=array();
			foreach ($data_profesores as $row) {
				$llaves[]=$row->id_asesorinterno;


			}
			$data_usuariosexternos=UsuariosExternos::model()->findAllByAttributes(array("id_usuario"=>$llaves));	
			$registros= array();

			foreach ($data_usuariosexternos as $row) {
				$item=new Profesores();
				$item->id_asesorinterno=$row->id_usuario;
				$item->nombres=$row->nombres;
				$item->apellidop=$row->apellidop;
				$item->apellidom=$row->apellidom;
				$item->activo=1;
				$item->id_usuario=$row->id_usuario;

				$registros[]=$item;
			}



			return $registros;
		}

}
